#ifndef SERVER_H
#define SERVER_H
#include "list.h"

//-------------------------------------------
// Server Lock Declaration
//-------------------------------------------

class ServerLock{
private:
	char * name;
	bool busy;
	int machineID;	//owner's machineID
	int mailboxID;	//owner's  mailbox
	List * waitQueue;

public :
	ServerLock(char * debugName);
	~ServerLock();
	char * getName(){return name;}
	char * Acquire(int mID, int bID); // return NULL if lock is not available
	char * Release(int mID, int bID); // return NULL if lock is available
	bool isHeldByCurrentNode(int mID, int bID);
	int getWaitLength(){return waitQueue->Count();}
};

struct  KernelServerLock
{
	ServerLock * lock;
	int useCount;
	bool isDeleted;
};

//-------------------------------------------
// Server Condition Variable Declaration
//-------------------------------------------

class ServerCV{
private:
	char * name;
	int lock;
	List * waitQueue;
public :
	ServerCV(char * debugName);
	~ServerCV();
	char * getName() {return name;}
	char * Wait(int lockID, int mID, int bID);
	char * Signal(int lockID, int mID, int bID);
	char * Broadcast(int lockID, int mID, int bID);
	int getWaitLength(){return waitQueue->Count();}
};


struct KernelServerCV
{
	ServerCV * cv;
	int useCount;
	bool isDeleted;
};

//-------------------------------------------
// Server Monitor Variable Declaration
//-------------------------------------------

class ServerMV{
private:
	char * name;
	int * data;
	int size;	
public :
	ServerMV(char * debugName, int array_size, int initValue);
	~ServerMV();
	char * getName(){return name;}
	int getSize(){return size;}
	int getValue(int idx){return data[idx];}
	void setValue(int idx, int value){data[idx] = value;}
	bool validateBondary(int idx){ return idx < size;}
};

struct KernelServerMV
{
	ServerMV * mv;
	int useCount;
	bool isDeleted;
};

#endif //SERVER_H
